package com.headfirst.BehaviorMode.templatemethod.sort;

/**
 * 让鸭子类实现Comparable接口，使其具备自然排序方法(通过鸭子的重量来比较大小)
 */
public class Duck implements Comparable<Duck> {

    private String name;
    private int weight;

    public Duck() {
    }

    public Duck(String name, int weight) {
        this.name = name;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String toString() {
        return name + " weighs " + weight;
    }

    @Override
    public int compareTo(Duck otherDuck) {
        if (this.weight < otherDuck.weight) {
            return -1;
        } else if (this.weight == otherDuck.weight) {
            return 0;
        } else { // this.weight > otherDuck.weight
            return 1;
        }
    }
}
